
/*=======[I N C L U D E S]====================================================*/
#include "FactoryMode_SeedKey.h"

/*=======[A P I   I M P L E M E N T A T I O N S]==============================*/
/*************************************************************************/
/*
 * Brief               This service calculated key accord with seed and keyK
 * Sync/Async          Synchronous
 * Reentrancy          Non Reentrant
 * Param-Name[in]      seed: Latest requested seed
 * Param-Name[in]      keyK: ECU permanent specific parameter for calculat-ing
 *                           the key, this value is provided by OEM
 * Param-Name[in/out]  None
 * Param-Name[out]     key:  Calculated key accord with seed and keyK
 * Return              SECM_OK 0x00:    Calculate success
 *                     SECM_NOT_OK 0x01:Calculate failure
 * PreCondition        None
 */
/*************************************************************************/
#define  Cycle_value 32
unsigned char FactoryMode_ComputeKey(unsigned long seed,unsigned long KeyK,unsigned long *key)
{
	unsigned char i;
	unsigned long  temp_key;
    
	/* Xor with KeyK */
	temp_key = seed ^ KeyK;
    
	/*calculate temp_key*/
    for (i = 0; i < Cycle_value ; i++)
    {
  		/* Rotate left 7 bits */
  		temp_key = ( temp_key << 12 ) | ( temp_key >> 20 );

  		/* Xor with KeyK */
  		temp_key ^= KeyK;
    }
    /*calculate key*/
    *key = temp_key;
    
    /*Return calculate status*/
    return OK;	
}
/*=======[E N D   O F   F I L E]==============================================*/

